package com.example.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;

public class XMLContentHandler extends DefaultHandler {

	//解析的person对象存储到list集合中  
    private List<VerInfo> verinfos;  
    
    public List<VerInfo> getVerinfos() {
		return verinfos;
	}

	public void setVerinfos(List<VerInfo> verinfos) {
		this.verinfos = verinfos;
	}

	//解析当前的person对象  
    public VerInfo verXml;  
    //声明标签的名称  
    public String tagName;  
      
      
    //获取解析所有的person对象  
    public List<VerInfo> getPersons() {  
        return verinfos;  
    }  

@Override  
public void characters(char[] ch, int start, int length)  
    throws SAXException {  
  
super.characters(ch, start, length);  
//首先判断tagName是否为空  
if(tagName!=null){
    String data=new String(ch,start,length);  
    //判断标签是否为空  
    if(tagName.equals("verinfo")){
    	
    }
    if(tagName.equals("versionName")){  
    	verXml.setVersionName(data);
    }else if(tagName.equals("versionCode")){//判断标签是否是age  
    	verXml.setVersionCode((short) Integer.parseInt(data));
    }else if(tagName.equals("describe")){
    	verXml.setDescribe(data);
    }
}  
}  

@Override  
public void endDocument() throws SAXException {  
  
super.endDocument();  
}  

@Override  
public void endElement(String uri, String localName, String qName)  
    throws SAXException {  
  
super.endElement(uri, localName, qName);  
//当person标签结束的时候把person对象存储到集合中   
if(localName.equals("verinfo")){  
    verinfos.add(verXml);  
    verXml=null;  
}  
  
this.tagName=null;  
}  

/** 
* 文档开始触发的事件 
*/  
@Override  
public void startDocument() throws SAXException {  
  
super.startDocument();  
verinfos=new ArrayList<VerInfo>();  
}  

@Override  
public void startElement(String uri, String localName, String qName,  
    Attributes attributes) throws SAXException {  
  
super.startElement(uri, localName, qName, attributes);  
//判断解析的标签是否是person  
if(localName.equals("verinfo")){  
    verXml=new VerInfo();  
}  
  
this.tagName=localName;  
}  
}
